package com.amber.proyecto.envia.imagenes.sw.usuario;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.amber.proyecto.envia.imagenes.sw.Principal;
import com.amber.proyecto.envia.imagenes.sw.R;
import com.amber.proyecto.envia.imagenes.sw.utils.Cifrado;
import com.amber.proyecto.envia.imagenes.sw.utils.Variables;

public class ActualizaDatosUsuario extends Activity{
	private Button btnCerrarSesion;
	private Button btnActualiza;
	private EditText etContra;
	private EditText etNombre;
	private EditText etRepContra;
	
	private Bundle bundle;
	

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.actualizadatosusuario);
		
		bundle = getIntent().getExtras();
		btnCerrarSesion = (Button)findViewById(R.id.btnCerrarSesPrinUsu);
		btnCerrarSesion.setOnClickListener(cerrarSesionPres);
		/*
		btnActualiza = (Button)findViewById(R.id.btnActualizaUsu);
		btnActualiza.setOnClickListener(btnActualizaPres);
		*/
		obtieneDatos();
	}
	
	private OnClickListener btnActualizaPres = new OnClickListener() {

		public void onClick(View v) {
			if (valida() == true){
				actualizaDatos();
				Toast.makeText(ActualizaDatosUsuario.this, "Datos actualizados", Toast.LENGTH_LONG).show();
			}
		}
	};
	
	private OnClickListener cerrarSesionPres = new OnClickListener() {
	
		public void onClick(View v) {
			Intent intent = new Intent();
    		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    		Variables.inicioSesion = false;
    		intent.setClass(ActualizaDatosUsuario.this, Principal.class);
       		startActivity(intent);
    		finish();
			
		}
	};
	
	private void obtieneDatos(){
		String URL = "http://"+Variables.HOST+"/pags/servicios.php";
		SoapObject request;
		String SOAP_ACTION="capeconnect:servicios:serviciosPortType#obtieneDatosUsuario"; 
		String METHOD_NAME = "obtieneDatosUsuario";
		String NAMESPACE = "http://www.your-company.com/servicios.wsdl";
		
		//etNombre = (EditText)findViewById(R.id.etNombreActualizaUsu);
		//etContra = (EditText)findViewById(R.id.etContraActualizaUsu);
		//etRepContra = (EditText)findViewById(R.id.etRepContraActualizaUsu);

	
		try{
					request = new SoapObject(NAMESPACE, METHOD_NAME); 
					request.addProperty("correoElectronico", bundle.getString("correoUsuario"));
				    
					SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
					
					envelope.dotNet = false;
					
					envelope.setOutputSoapObject(request);
					

					HttpTransportSE aht = new HttpTransportSE(URL); 

					aht.call(SOAP_ACTION, envelope);
					
					SoapObject result =  (SoapObject) envelope.getResponse();
					
	                etNombre.setText(((SoapPrimitive)result.getProperty(0)).toString());
	                etContra.setText(((SoapPrimitive)result.getProperty(1)).toString());
	                etRepContra.setText(((SoapPrimitive)result.getProperty(2)).toString());
	                
                

		    } 
	    catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
	}
	
	private void actualizaDatos(){
		String URL = "http://"+Variables.HOST+"/pags/servicios.php";
		SoapObject request;
		String SOAP_ACTION="capeconnect:servicios:serviciosPortType#actualizaUsuario"; 
		String METHOD_NAME = "actualizaUsuario";
		String NAMESPACE = "http://www.your-company.com/servicios.wsdl";		
		try{
			request = new SoapObject(NAMESPACE, METHOD_NAME); 
			request.addProperty("nombreUsuario", etNombre.getText().toString());
			request.addProperty("contraUsuario", Cifrado.getStringMessageDigest(etContra.getText().toString(),Cifrado.SHA256));
		    
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			
			envelope.dotNet = false;
			
			envelope.setOutputSoapObject(request);
			

			HttpTransportSE aht = new HttpTransportSE(URL); 

			aht.call(SOAP_ACTION, envelope);
			
			SoapObject result =  (SoapObject) envelope.bodyIn;
            SoapPrimitive spResul = (SoapPrimitive) result.getProperty("result");
            
			Log.i("resultado",spResul.toString());
			Toast.makeText(ActualizaDatosUsuario.this, "Datos actualizados", Toast.LENGTH_LONG).show();
	    } 
		catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		
		finally{
			
		}
		
	}
	
	private boolean valida(){
		if (!etContra.getText().toString().equals(etRepContra.getText().toString())){
			Toast.makeText(ActualizaDatosUsuario.this, "Las contraseñas no coinciden", Toast.LENGTH_LONG).show();
			return false;
		}
		
		if (etNombre.getText().length() == 0){
			Toast.makeText(ActualizaDatosUsuario.this, "Faltan datos", Toast.LENGTH_LONG).show();
			return false;
		}
		
		return true;
	}

}
